//app.js
App({
  data: {
    apiDomain: 'https://api.su.cas.cx/',
    //apiDomain: 'http://wechat.api.rent.com/',
    imgDomain: 'https://api.su.cas.cx/image/',
    //imgDomain: 'http://image.rent.com/',
    api: {
      wholeItems: '/wechat.whole.items',
      infoById: '/wechat.whole.info.by.id',
      token: '/wechat.token',
      changeToken: '/wechat.token.change',
      memberInfo: '/wechat.member.info',
      wholeAppointment: '/wechat.whole.appointment',
      wholeAppointmentItems: '/wechat.whole.appointment.items',
      wholeAppointmentDelete: '/wechat.whole.appointment.delete'
    }
  },
  onLaunch: function () {
    this.getUserInfo()
  },
  /**
   * 网络请求
   */
  request (method = 'get', api, data, callback, fail) {
    let url = this.data.apiDomain + this.data.api[api]
    let options = {
      url: url,
      data: data,
      header: {
        'Content-Type': 'application/json',
        'Token': wx.getStorageSync('token')
      },
      method: method,
      dateType: 'json',
      success: (res) => {
        wx.hideLoading()
        if (res.statusCode !== 200) {
          wx.showToast({
            title: res.errMsg,
            duration: 2000,
            icon: 'none'
          })
          return false
        }
        if (typeof res.data.code === 'undefined') {
          wx.showToast({
            title: '接口未返回状态码!',
            duration: 2000,
            icon: 'none'
          })
          return false
        }
        if (res.data.code !== 10000) {
          wx.showToast({
            title: res.data.message,
            duration: 2000,
            icon: 'none'
          })
          return false
        }
        if (typeof callback === 'function') {
          callback(res.data.data, res.data.message)
        }
      },
      fail: (err) => {
        wx.hideLoading()
        if (typeof fail === 'function') {
          fail(err)
        }
      }
    }
    wx.request(options)
  },
  /**
   * 公共数据
   */
  globalData: {
    userInfo: null
  },
  /**
   * @purpose 获取用户信息
   */
  getUserInfo (callback) {
    let token  = wx.getStorageSync('token')
    if (token) {
      wx.showLoading()
      this.request('get', 'memberInfo', {}, res => {
        this.globalData.userInfo = res
        if (typeof callback === 'function') {
          callback(res)
        }
      })
    }
  },
  /**
   * 登录
   */
  login(callback) {
    // 登录小程序获取code
    wx.login({
      success: (res) => {
        let data = {
          code: res.code
        }
        if (res.code) {
          // 登录成功获取用户信息
          wx.getUserInfo({
            lang: 'zh_CN',
            withCredentials: true,
            success: (res) => {
              // 获取成功将数据发送到后端进行解码
              data.userInfo = res.userInfo
              data.signatrue = res.signature
              data.iv = res.iv
              data.encryptedData = res.encryptedData
              // 解码并获取token
              this.request('post', 'token', data, (res) => {
                // 本地缓存token
                wx.setStorageSync('token', res.token)
                // 获取用户信息
                this.getUserInfo(callback)
              })
            },
            fail: (err) => {
              console.log(err)
            },
            complete: () => {
            }
          })
        } else {
          wx.showToast({
            title: '登录失败！',
          })
        }
      }
    })
  }
})